/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.mojo.flex.generator;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

import java.util.HashSet;
import java.util.Set;


/**
 * @author Izzet_Mustafayev
 *
 */
public class JavaMethod extends org.granite.generator.as3.reflect.JavaMethod {
    private Set<String> imports = new HashSet<String>();
    public Set<JavaArgument> arguments = new HashSet<JavaArgument>();

    public JavaMethod(Method method, MethodType type) {
        super(method, type);
        getReturnType();
        arguments = getArguments();
    }

    public Set<String> resolveImports() {
        return imports;
    }

    public Set<JavaArgument> getArguments() {
        Set<JavaArgument> arguments = new HashSet<JavaArgument>();
        Type[] parameters = getMember().getGenericParameterTypes();

        if (null != parameters) {
            String argumentName = "arg";

            for (int i = 0; i < parameters.length; i++) {
                arguments.add(new JavaArgument(argumentName + i, resolveType(parameters[i].toString())));
            }
        }

        return arguments;
    }

    private String resolveType(final String type) {
        String resolvedType = "";
        resolvedType = type.replace("class ", "");

        boolean imported = false;

        if (resolvedType.contains("java.util.List")) {
            resolvedType = "ArrayCollection";
            addImport("mx.collections.ArrayCollection");
            imported = true;
        }

        if (resolvedType.contains("java.lang.String")) {
            resolvedType = "String";
            imported = true;
        }
        
        if (resolvedType.contains("boolean") || resolvedType.contains("java.lang.Boolean")) {
            resolvedType = "Boolean";
            imported = true;
        }

        if (resolvedType.contains("java.lang.Long")) {
            resolvedType = "Number";
            imported = true;
        }

        if (!imported) {
            addImport(resolvedType);
            resolvedType = resolvedType.substring(resolvedType.lastIndexOf(".") + 1, resolvedType.length());
        }

        return resolvedType;
    }

    public String getReturnType() {
        String returnType = "";
        Type resType = getMember().getGenericReturnType();

        if ("void".equals(resType.toString())) {
            returnType = "void";
        }

        if (!"void".equals(resType.toString())) {
            returnType = resolveType(resType.toString());
        }

        return returnType;
    }

    private void addImport(String importItem) {
        imports.add(importItem);
    }
}
